package org.kailu.book.controller;

import org.kailu.book.entity.StationeryEntity;
import org.kailu.book.service.StationeryService;
import org.kailu.book.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * @type viet.ch
 * @generated May 25, 2014
 * @from Ho Chi Minh, VN
 */

@Controller
public class StationeryController {

	@Autowired
	private StationeryService stationeryService;
	
	@Autowired
	private TypeService typeService;
	
	@RequestMapping("/stationery")
	public String getData(ModelMap map) {
		map.addAttribute("stationeryList", stationeryService.getAll());
		return "stationery";
	}
	
	@RequestMapping(value = "/stationery/saveorupdate", method = RequestMethod.GET)
	public String initData(ModelMap map) {
		map.addAttribute("stationery",new StationeryEntity());
		map.addAttribute("typeList", typeService.getAll());
		return "stationery_new";
	}
	
	@RequestMapping(value = "/stationery/saveorupdate", method = RequestMethod.POST)
    public String save(@ModelAttribute(value="stationery") StationeryEntity stationeryEntity, BindingResult result)
    {
		stationeryService.saveOrUpdate(stationeryEntity);
		return "redirect:/stationery";
    }
	
	@RequestMapping(value = "/stationery/saveorupdate/{stationeryId}", method = RequestMethod.GET)
    public String update(@PathVariable("stationeryId") int stationeryId, ModelMap map)
    {
		map.addAttribute("stationery", stationeryService.get(stationeryId));
		map.addAttribute("typeList", typeService.getAll());
		return "stationery_new";
    }
	
	@RequestMapping(value = "/stationery/delete/{stationeryId}", method = RequestMethod.GET)
    public String delete(@PathVariable("stationeryId") int stationeryId)
    {
		this.stationeryService.delete(stationeryId);
		return "redirect:/stationery";
    }
}
